package com.yoomiito.app.ui.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.yoomiito.app.Constant;
import com.yoomiito.app.ImmersionBarFragment;
import com.yoomiito.app.MainActivity;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.bean.NavInfo;
import com.yoomiito.app.present.home.HomePresenter;
import com.yoomiito.app.ui.search.SearchActivity;
import com.yoomiito.app.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.ColorUtils;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.router.Router;

/**
 * 首页
 */
public class HomeFragment extends ImmersionBarFragment<HomePresenter> {


    @BindView(R.id.fragment_container)
    FrameLayout mFrameLayout;
    @BindView(R.id.top_search)
    LinearLayout top_search;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.rl_dot)
    RelativeLayout rlDot;
    @BindView(R.id.magicIndicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.top_search_ll)
    LinearLayout top_search_ll;
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();
    private List<NavInfo> mNavInfos;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public HomePresenter newP() {
        return new HomePresenter(App.mAppConfig);
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        ImmersionBar.setTitleBar(context, top_search);

        mFragmentContainerHelper.handlePageSelected(0, false);
//        if (UserInfoConfig.getCompanyId() == 1) {
//            //获取导航栏
//            magicIndicator.setVisibility(View.VISIBLE);
//        }else {
//            magicIndicator.setVisibility(View.GONE);
//        }
//        getP().getNavigates("");
        mFragments.add(new HomeRecommendFragment());
        HomeRecommendFragment homeRecommendFragment = (HomeRecommendFragment)mFragments.get(0);
        homeRecommendFragment.changeTopSearchBg();
        ((MainActivity)context).changeYouMiIv(homeRecommendFragment.showYouIcon);
        switchPages(0);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
    }

    private void switchPages(int index) {

        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment;
        for (int i = 0, j = mFragments.size(); i < j; i++) {
            if (i == index) {
                continue;
            }
            fragment = mFragments.get(i);
            if (fragment.isAdded()) {
                fragmentTransaction.hide(fragment);
            }
        }
        fragment = mFragments.get(index);
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(R.id.fragment_container, fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
    }

    private void initMagicIndicator1(final List<NavInfo> navInfos) {
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setSkimOver(false);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return navInfos.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(navInfos.get(index).getName());
                simplePagerTitleView.setTextSize(18);
                simplePagerTitleView.setNormalColor(Color.WHITE);
                simplePagerTitleView.setSelectedColor(Color.WHITE);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mFragmentContainerHelper.handlePageSelected(index);
                        if (index == 0) {
                            top_search_ll.setBackgroundColor(Color.TRANSPARENT);
                            //修改底部bar图标
                            HomeRecommendFragment homeRecommendFragment = (HomeRecommendFragment)mFragments.get(0);
                            homeRecommendFragment.changeTopSearchBg();
                            ((MainActivity)context).changeYouMiIv(homeRecommendFragment.showYouIcon);
//                            magicIndicator.setBackgroundColor(Color.TRANSPARENT);
                        } else {
                            //底部bar显示优图标
                            ((MainActivity)context).changeYouMiIv(false);
                            top_search_ll.setBackgroundColor(ColorUtils.blendARGB(Color.TRANSPARENT,
                                    ContextCompat.getColor(context, R.color.color_FF035B), 1));
//                            magicIndicator.setBackgroundColor(ColorUtils.blendARGB(Color.TRANSPARENT,
//                                    ContextCompat.getColor(context, R.color.color_ff5100), 1));
                        }
                        switchPages(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        mFragmentContainerHelper.attachMagicIndicator(magicIndicator);
    }

    private void initFragments(List<NavInfo> navInfos) {
        mFragments.clear();
        for (int i = 0; i < navInfos.size(); i++) {
            if (i == 0) {
                HomeRecommendFragment homeRecommendFragment = new HomeRecommendFragment();
//                HomeRecommendFragment_1 homeRecommendFragment = new HomeRecommendFragment_1();
                mFragments.add(homeRecommendFragment);
            } else {
                HomeNavFragment homeNavFragment = new HomeNavFragment();
                Bundle bundle = new Bundle();
                bundle.putString(Constant.TYPE, navInfos.get(i).getName());
                bundle.putString(Constant.ID, String.valueOf(navInfos.get(i).getId()));
                homeNavFragment.setArguments(bundle);
                mFragments.add(homeNavFragment);
            }
        }
       /* XFragmentAdapter fragmentAdapter = new XFragmentAdapter(getChildFragmentManager(),mFragments,null);
        mScrollViewPager.setAdapter(fragmentAdapter);
        mScrollViewPager.setAdapter(fragmentAdapter);
        mScrollViewPager.setOffscreenPageLimit(mFragments.size());
        mScrollViewPager.setScroll(false);*/
    }

    @OnClick({R.id.tv_search, R.id.rl_dot})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_search:
                Router.newIntent(context)
                        .to(SearchActivity.class)
                        .launch();
                break;
            case R.id.rl_dot:
                break;
        }
    }

    /**
     * @param navInfos
     */
    public void getNavInfos(List<NavInfo> navInfos) {
        NavInfo navInfo = new NavInfo();
        navInfo.setName("推荐");
        navInfos.add(0, navInfo);
        initMagicIndicator1(navInfos);
        initFragments(navInfos);
        switchPages(0);
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this).keyboardEnable(false).init();
    }
}
